//
//  CYYNetWorkManager+GetSomeUserParties.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/9.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+GetSomeUserParties.h"

@implementation CYYNetWorkManager (GetSomeUserParties)

//根据自己（id）查询该用户的【所有发起的活动】(未过期)
+ (void)getMySponsorPartiesSucessGet:(SucessGet)sucessGet
                                      fail:(Fail)fail{
    if ([[UserModel defaultUser] isAlreadyLoad]) {
        NSString * mobilePhoneNumber = [CYYNetWorkManager currentUserGet].mobilePhoneNumber;
        BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
        //构造约束条件
        BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
        [inQuery whereKey:@"mobilePhoneNumber" equalTo:mobilePhoneNumber];
        
        //匹配查询
        [bquery whereKey:@"partySponsor" matchesQuery:inQuery];
        [bquery whereKey:@"partyState" notEqualTo:@"已取消"];
        //约束2
        //获得当前系统的时间
        NSDate * currentDate = [NSDate date];
        //时间转换成NSString
        NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
        [dateFormate setDateFormat:@"yyyy/MM/dd"];
        NSString * currentDateString = [dateFormate stringFromDate:currentDate];
        [bquery whereKey:@"partyEndTime" greaterThanOrEqualTo:currentDateString];
        
        //降序
        [bquery orderByAscending:@"partyBeginTime"];
        
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (error) {
                fail(error);
            }else{
                sucessGet(array);
            }
        }];
    }else{
        //用户没有登陆
//        NSLog(@"当前用户没有登陆");
    }
}

//根据某个用户(该用户的手机号)查询该用户的【所有发起的活动】（未过期）
+ (void)getSomeUserSponsorParties:(NSString *)mobilePhoneNumber
                        sucessGet:(SucessGet)sucessGet
                             fail:(Fail)fail{
    BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
    //构造约束条件
    BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
    [inQuery whereKey:@"mobilePhoneNumber" equalTo:mobilePhoneNumber];
    
    //匹配查询
    [bquery whereKey:@"partySponsor" matchesQuery:inQuery];
    //约束2
    //获得当前系统的时间
    NSDate * currentDate = [NSDate date];
    //时间转换成NSString
    NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
    [dateFormate setDateFormat:@"yyyy/MM/dd"];
    NSString * currentDateString = [dateFormate stringFromDate:currentDate];
    [bquery whereKey:@"partyEndTime" greaterThanOrEqualTo:currentDateString];
    
    //降序
    [bquery orderByDescending:@"partyBeginTime"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            fail(error);
        }else{
            sucessGet(array);
        }
    }];
}


@end
